#include "stdafx.h"
#include "POSCheck.h"
#include "DlgLDCheck.h"
#include "LineDisplay.h"

using namespace Utility::LoggerLib;

//IMPLEMENT_DYNAMIC(CDlgLDCheck, CDialog)


BEGIN_MESSAGE_MAP(CDlgLDCheck, CDialog)
    ON_BN_CLICKED(IDOK, &CDlgLDCheck::OnBnClickedOk)
    ON_MESSAGE(WM_INIT_LDCHECK_DLG, OnInitAdditionItems)
  //  ON_MESSAGE(WM_INIT_LDCHECK_DLG, &CDlgLDCheck::OnInitAdditionItems)
  ON_BN_CLICKED(IDC_BTN_DES1, &CDlgLDCheck::OnBnClickedBtnDes1)
  ON_BN_CLICKED(IDC_BTN_DES2, &CDlgLDCheck::OnBnClickedBtnDes2)
  ON_BN_CLICKED(IDC_BTN_DES3, &CDlgLDCheck::OnBnClickedBtnDes3)
  ON_BN_CLICKED(IDC_BTN_DES4, &CDlgLDCheck::OnBnClickedBtnDes4)
  ON_BN_CLICKED(IDC_BTN_DES5, &CDlgLDCheck::OnBnClickedBtnDes5)
  ON_BN_CLICKED(IDC_RADIO_NORMAL, &CDlgLDCheck::OnBnClickedRadioNormal)
  ON_BN_CLICKED(IDC_RADIO_ERROR, &CDlgLDCheck::OnBnClickedRadioError)
  ON_BN_CLICKED(IDC_RADIO_PASS, &CDlgLDCheck::OnBnClickedRadioPass)
END_MESSAGE_MAP()

CDlgLDCheck::CDlgLDCheck(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgLDCheck::IDD, pParent)
{
    m_pLineDisplay = NULL;
    m_nStatus = ES_Unchecked;
    m_bPT = FALSE;
}

CDlgLDCheck::~CDlgLDCheck()
{
	
    delete m_pLineDisplay;
}

void CDlgLDCheck::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
}

void CDlgLDCheck::SetLineDisplayInfo(int nType, int nPort)
{
    switch(nType)
    {
    case VC220:
        m_pLineDisplay = new CVC220(nPort);
        break;
    case VC110:
        m_pLineDisplay = new CVC110(nPort);
        break;
    default:
        break;
    }
    
}

BOOL CDlgLDCheck::OnInitDialog()
{
    CDialog::OnInitDialog();
    
    CEdit *pEdit = (CEdit*)GetDlgItem(IDC_EDIT_CHECK_CONTENT);
    if (NULL != pEdit)
    {
        pEdit->SetFocus();
    }

    if (NULL == m_pLineDisplay) return FALSE;   // zhwang: to do show initialize line display configuration

    if (FALSE == m_pLineDisplay->Initialize())
    {
        MessageBox(_T("Fail to initialize linedisplay, please check!"), L"Error", MB_ICONERROR|MB_OK);
    }

    GetDlgItem(IDC_BTN_DES1)->EnableWindow(FALSE);
    GetDlgItem(IDC_BTN_DES2)->EnableWindow(FALSE);
    GetDlgItem(IDC_BTN_DES3)->EnableWindow(FALSE);
    GetDlgItem(IDC_BTN_DES4)->EnableWindow(FALSE);
    GetDlgItem(IDC_BTN_DES5)->EnableWindow(FALSE);


    GetDlgItem(IDC_RADIO_NORMAL)->EnableWindow(m_bPT);
    GetDlgItem(IDC_RADIO_ERROR)->EnableWindow(m_bPT);
    GetDlgItem(IDC_RADIO_PASS)->EnableWindow(m_bPT);
    GetDlgItem(IDOK)->EnableWindow(!m_bPT);

    switch(m_pLineDisplay->GetType())
    {
    case VC110:
        PostMessage(WM_INIT_LDCHECK_DLG);
        break;

    default:
        break;
    }
    
    return TRUE;
}

BOOL CDlgLDCheck::PreTranslateMessage(MSG* pMsg)
{
    if( pMsg->message == WM_CHAR) 
    {
        static int nCurLine = 1;
        char szCharacter[50] = "";
//        int  iKey = 0x30;
        memset(szCharacter, pMsg->wParam, 20);
        CString strInput(szCharacter);
        SetDlgItemText(IDC_EDIT_CHECK_CONTENT, strInput/*(LPCTSTR)szAmount*/);
        if (NULL == m_pLineDisplay) 
            return CDialog::PreTranslateMessage(pMsg);

        switch(m_pLineDisplay->GetType())
        {
        case VC220:	
            m_pLineDisplay->DisplayText((LPCTSTR)szCharacter, 20, nCurLine);
            nCurLine = (nCurLine == 1)? 2 : 1;
            break;

        case VC110:
            if(pMsg->wParam >= 0x30 && pMsg->wParam <=  0x39)
            {
                //Utility::LoggerLib::Log(Utility::LoggerLib::ERROR_LEVEL, Utility::LoggerLib::EPOSChecker, _T("%s, %s, %d."), _T(__FILE__), _T(__FUNCTION__), __LINE__);
                Log(DEBUG_LEVEL, EPOSChecker, _T("Check VC110, char input in the range."));
     //           m_pLineDisplay->DisplayText((LPCTSTR)szCharacter, 10, nCurLine);
				m_pLineDisplay->DisplayTextEx(szCharacter, 10);
            }
            break;

        default:
            break;
        }
    }

    return CDialog::PreTranslateMessage(pMsg);
}

void CDlgLDCheck::OnBnClickedOk()
{
    if (NULL != m_pLineDisplay)
    {
        m_pLineDisplay->Close();
    }

    GetParent()->PostMessage(WM_PT_LD, m_nStatus);

    OnOK();
}

LRESULT CDlgLDCheck::OnInitAdditionItems(WPARAM wParam, LPARAM lParam)
{
    GetDlgItem(IDC_BTN_DES1)->SetWindowTextW(_T("Off"));
    GetDlgItem(IDC_BTN_DES2)->SetWindowTextW(_T("UnitPrice"));
    GetDlgItem(IDC_BTN_DES3)->SetWindowTextW(_T("Total"));
    GetDlgItem(IDC_BTN_DES4)->SetWindowTextW(_T("Pay"));
    GetDlgItem(IDC_BTN_DES5)->SetWindowTextW(_T("Change"));

    GetDlgItem(IDC_BTN_DES1)->EnableWindow(TRUE);
    GetDlgItem(IDC_BTN_DES2)->EnableWindow(TRUE);
    GetDlgItem(IDC_BTN_DES3)->EnableWindow(TRUE);
    GetDlgItem(IDC_BTN_DES4)->EnableWindow(TRUE);
    GetDlgItem(IDC_BTN_DES5)->EnableWindow(TRUE);

    return S_OK;
}

void CDlgLDCheck::OnBnClickedBtnDes1()
{
    if(NULL != m_pLineDisplay)
        m_pLineDisplay->ClearDescriptor();
}


void CDlgLDCheck::OnBnClickedBtnDes2()
{
    if(NULL != m_pLineDisplay)
        m_pLineDisplay->SetDescriptor(1);
}



void CDlgLDCheck::OnBnClickedBtnDes3()
{
    if(NULL != m_pLineDisplay)
        m_pLineDisplay->SetDescriptor(2);
}


void CDlgLDCheck::OnBnClickedBtnDes4()
{
    if(NULL != m_pLineDisplay)
        m_pLineDisplay->SetDescriptor(3);
}


void CDlgLDCheck::OnBnClickedBtnDes5()
{
    if(NULL != m_pLineDisplay)
        m_pLineDisplay->SetDescriptor(4);
}


void CDlgLDCheck::OnBnClickedRadioNormal()
{
    m_nStatus = ES_Normal;
    GetDlgItem(IDOK)->EnableWindow();
}


void CDlgLDCheck::OnBnClickedRadioError()
{
    m_nStatus = ES_Exception;
    GetDlgItem(IDOK)->EnableWindow();
}




void CDlgLDCheck::OnBnClickedRadioPass()
{
    m_nStatus = ES_Pass;
    GetDlgItem(IDOK)->EnableWindow();
}
